package cn.icare.cms;

import org.joda.time.DateTime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 当前时间格式化为yyyyMMddHHmmssSSS,精确到毫秒
     *
     * @return
     */
    public static String getCurrentDatetime() {
        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
    }

    /**
     * 将时间格式转化为年-月-日格式的字符串
     *
     * @param date
     * @return
     */
    public static String formateToDate(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    /**
     * 将时间格式转化为年-月-日格式的字符串
     *
     * @param date
     * @return
     */
    public static String formateToDateTime(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 获取当前时间截，精确到秒
     *
     * @return
     */
    public static String getCurrentDateStamp() {
        String timestamp = String.valueOf(new Date().getTime() / 1000);
        return timestamp;
    }

    /**
     * 日期时间-相加，单位：分
     *
     * @param date
     * @param mins
     * @return
     */
    public static Date addMiniter(Date date, int mins) {
        DateTime dateTime = new DateTime(date);
        DateTime d = dateTime.plusMinutes(mins);
        return d.toDate();
    }

    /**
     * 日期时间-相加，单位：秒
     *
     * @param date
     * @param second
     * @return
     */
    public static Date addSecond(Date date, int second) {
        DateTime dateTime = new DateTime(date);
        DateTime d = dateTime.plusSeconds(second);
        return d.toDate();
    }

    /**
     * 获取今天是周几
     *
     * @return
     */
    public static int getWeekday() {
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday == 1)
            return 7;
        else return weekday - 1;
    }

    /**
     * 日期时间-相加，单位：分
     *
     * @param date
     * @param i
     * @return
     */
    public static Date addDay(Date date, int i) {
        DateTime dateTime = new DateTime(date);
        DateTime d = dateTime.plusDays(i);
        return d.toDate();
    }


    /**
     * 字符转日期
     *
     * @param date
     * @param
     * @return
     */
    public static Date toDate(String date, String pattern) {


        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
